package com.donquijote.spring14.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author 421527548@qq.com
 * @version V1.0
 * @title
 * @description
 * @date 2022-11-29 10:44
 */
@Component("logAspect")
@Aspect
@Order(2)
public class LogAspect {

//    @Before("execution(* com.donquijote.spring14.service.UserService.*(..))")
    /*@Before("execution(* com.donquijote.spring14.service.OrderService.add*())")
    public void beforeCode(){
        System.out.println("..增强代码..");
    }*/

    @Pointcut("execution( * com.donquijote.spring14.service..*(..))")
    public void generalPoint(){

    }

    //前置通知
    @Before("generalPoint()")
    public void beforeAdvice(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.getName());
        System.out.println("前置通知");
    }

    //后置通知
    @AfterReturning("generalPoint()")
    public void afterReturnAdvice(){
        System.out.println("后置通知");
    }

//（3）环绕通知：@Around目标方法之前通知，同时对目标方法执行之后通知
    @Around("generalPoint()")
    public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("前置环绕通知");
        joinPoint.proceed();//执行目标
        System.out.println("后置环绕通知");
    }

//（4）异常通知：@AfterThrowing发生异常之后执行的通知
    @AfterThrowing("generalPoint()")
    public void afterThrowing(){
        System.out.println("发生了异常的通知");
    }


//（5）最终通知：@After放在finally语句块中的通知
    @After("generalPoint()")
    public void afterAdvice(){
        System.out.println("最终通知");
    }
}
